/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using System.Collections.Generic;
using NetProtocol.POD;
using NetProtocol.Enum;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay;
using IQIGame.Onigao.Framework;
using System.Threading.Tasks;
using System;

namespace NetProtocol.Server {
	/// <summary>
	/// 投票活动
	/// module : operationsVote
	/// </summary>
	public interface SNetOperationsVote : INetServerModule {
		Dictionary<int, MessageHandler> INetServerModule.GetModuleFunction()
        {	
            return new()
            {
                { NetMessageId.operationsVote_vote, (session, data) =>
		            {
#if ENABLE_NET_LOG || UNITY_EDITOR
						NetProtocolHelper.ServerLog(NetMessageId.operationsVote_vote, "Recv->" + data.ToString());
#endif
		                VoteRequest _data = (VoteRequest)data;
						return CS_vote(session, _data.eventId, _data.voteId);
		            } 
            	},
            };
        }
        
        Dictionary<int, Func<IReceiveMessage>> INetServerModule.GetModuleRequest() 
        {
	        return new ()
	        {
	        	{NetMessageId.operationsVote_vote, ()=> new VoteRequest()},
	        };
	    }
        
#region Server-side Interface
		/// <summary>
        ///  投票
        /// </summary>
	    protected VoteResult CS_vote(ISession session, int eventId, int voteId);
#endregion
#region Client-side Interface
		/// <summary>
        /// 
        /// </summary>
        public VoteResult SC_voteResult(Task<VoteResult> future){return new VoteResult(future);}
        public VoteResult SC_voteResult(int code, int eventId, int voteId, List<ItemShowPOD> getItems){return new VoteResult(code, eventId, voteId, getItems);}
        public VoteResult SC_voteResult(int code){return new VoteResult(code);}
#endregion

#region Messages
	public class VoteResult : MessageResult {
	    private int eventId;
	    private int voteId;
	    private List<ItemShowPOD> getItems;
	    public VoteResult(Task<VoteResult> future): base(future) { }
		public VoteResult(int code): base(code) { }
		public new VoteResult SetDelayCall(Action delayCall) { base.SetDelayCall(delayCall); return this; }
	    internal VoteResult(int code, int eventId, int voteId, List<ItemShowPOD> getItems){
	        this.code = code;
	        this.eventId = eventId;
	        this.voteId = voteId;
	        this.getItems = getItems == null ? null : new (getItems);
	    }
        public override MessageResult GetFutureResult(Task task) { return ((Task<VoteResult>)task).Result; }
		public override int GetMessageId() {return NetMessageId.operationsVote_voteResult;}
		public override SendPackage ToPackage() {
			SendPackage si_p = NetProtocolHelper.GetSendPackage();
			si_p.Header.Messagid = NetMessageId.operationsVote_voteResult;
			si_p.StartWrite();
			SOutputStream _out = si_p.OutputStream;
			_out.Write_int(code);
			_out.Write_int(eventId);
			_out.Write_int(voteId);
			_out.Write_list_pod(getItems);
			return si_p;
		}
		public override string ToString() {
    		return "OperationsVote.SC_voteResult { "
			+ "code="
			+ code
			+ ", eventId="
			+ eventId
			+ ", voteId="
			+ voteId
			+ ", getItems="
			+ getItems.ToString<ItemShowPOD>()
			+" }";
		}
	}
	private class VoteRequest : IReceiveMessage
    {
		internal int eventId;
		internal int voteId;
        public void FromPackage(ReceivePackage package)
		{
			SInputStream _in = package.InputStream;
            eventId = _in.HasRemaining() ? _in.Read_int() : default;
            voteId = _in.HasRemaining() ? _in.Read_int() : default;
		}
        public int GetMessageId() { return NetMessageId.operationsVote_vote; }
        public override string ToString()
        {
            return "OperationsVote.vote"+ ", eventId="+ eventId+ ", voteId="+ voteId;
        }
    }
	}
#endregion
}